//
//  ViewXingCheng.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "ViewXingCheng.h"
#import "CellOfxingcheng.h"
#import <AFNetworking.h>
#import "ModelOfEssay.h"
#import "MdoelOfxingcheng.h"
@interface ViewXingCheng ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tabviewxingcheng;



@property (nonatomic,retain)NSMutableDictionary *dic;


@property (nonatomic,retain)NSArray *array;
@end

@implementation ViewXingCheng




-(void)dealloc{
   
    [_dic release];
    [_array release];
    [_tabviewxingcheng release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        

        self.tabviewxingcheng = [[UITableView alloc]initWithFrame:CGRectMake(0, 42, frame.size.width, frame.size.height - 42) style:UITableViewStylePlain];
        
        [self addSubview:self.tabviewxingcheng];
        self.tabviewxingcheng.delegate =self;
        self.tabviewxingcheng.dataSource = self;
        [self.tabviewxingcheng registerClass:[CellOfxingcheng class]forCellReuseIdentifier:@"pool1"];
        
        self.tabviewxingcheng.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tabviewxingcheng.backgroundColor = [UIColor blackColor];
        [self.tabviewxingcheng release];
        
    }
    return self;
}

- (void)handleDataWith:(NSString *)str{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSArray *arrTripDays = [responseObject objectForKey:@"trip_days"];
            self.dic = [NSMutableDictionary dictionary];
            
            for (NSDictionary *dic in arrTripDays) {
                NSNumber *day = [dic objectForKey:@"day"];
                NSInteger dayInt = [day integerValue];
                NSString *date = [dic objectForKey:@"trip_date"];
                NSString *key = [NSString stringWithFormat:@"DAY%02ld  %@",dayInt,date];
                NSArray *arrNodes = [dic objectForKey:@"nodes"];
                
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *dicInNodes in arrNodes) {
                   
                    NSString *entry_name = [dicInNodes objectForKey:@"entry_name"];
                    
//                    NSLog(@"%@",entry_name);
                    
                    MdoelOfxingcheng *model = [[MdoelOfxingcheng alloc]init];
                    model.entry_name = entry_name;
                    
                    [array addObject:model];
                    
                    [model release];
                    
                }
                
                [self.dic setObject:array forKey:key];
            }
            self.array = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
            [self.tabviewxingcheng reloadData];
            
            
        }

    }];
    [dataTask resume];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *key = [self.array objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:key];
    
    
    return arr.count -1;
    
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.array objectAtIndex:section];
    
    
    
    return key;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [self.array objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
        CellOfxingcheng *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
//    NSLog(@"%@",key);
//    NSLog(@"%@",arr);
    
    MdoelOfxingcheng *model = [arr objectAtIndex:indexPath.row + 1];
    
    
    cell.decsxingcheng.text = [NSString stringWithFormat:@"        %@",model.entry_name];
   
   
    
    return cell;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 30;
    }



@end
